package com.yushixin.account.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yushixin.account.model.Account;
import com.yushixin.account.service.MQProducerService;
import com.yushixin.common.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * MQ生产者实现类
 */
@Service
@Slf4j
public class MQProducerServiceImpl implements MQProducerService {
    
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    @Value("${rocketmq.producer.send-message-timeout}")
    private long sendMessageTimeout;
    
    @Override
    public void sendTagMassage(String tag, Account account) {
        String json = JSONObject.toJSONString(account);
        SendResult sendResult = rocketMQTemplate.syncSend(Constant.MQTopic + ":" + tag, json, sendMessageTimeout);
        log.info("生产者,发送[{}]消息[{}],结果:[{}]", tag, json, sendResult.getSendStatus().name());
    }
}